﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Data;
using HKB.Data.Bll;
using HKB.Web;
using HKB.Web.Shop.Member;

public partial class Member_Index : Member
{
    private Shop_City _mycity = null;
    protected Shop_City mycity
    {
        get
        {
            Shop_City mycity;
            if (this._mycity == null)
            {
                this._mycity = new Shop_City(this.conn);
                mycity = this._mycity;
            }
            else
            {
                mycity = this._mycity;
            }
            return mycity;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            //this.shopProduct = new Shop_Product();
            this.lblUserName.Text = base.user.UserName;
            DataTable dataTable = BLLInstance.BllShoppingCarts.GetList(" userID=" + base.user.UserID).Tables[0];
            this.lblShopCarNum.Text = dataTable.Rows.Count.ToString();
            this.GetRemindMe();

            this.BindProList();
            this.BindPages();
        }
    }

    protected void GetRemindMe()
    {
        int userID = base.user.UserID;
        string text = DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00";

        string str = string.Concat(new object[]
		{
			" (userid=",
			userID,
			" and inputTime>'",
			text,
			"'  and hlshop_order.OrderFormID=HLShop_OrderItem.OrderFormID)"
		});
        string str2 = string.Concat(new object[]
		{
			" or (inputTime>'",
			text,
			"0' and userid=",
			userID,
			" and DateDiff(s,UpdateTime,getdate())>=0 and hlshop_order.OrderFormID=HLShop_OrderItem.OrderFormID)"
		});
        string sQLString = "select hlshop_order.OrderFormID,hlshop_order.OrderFormNum,Status,DeliverStatus,PayStatus,BuyerReview,InputTime from hlshop_order,HLShop_OrderItem where " + str + str2;

        int count = 0;
        DataTable dataTable = DbHelperSQL.GetDataTable(sQLString);
        if (dataTable != null && dataTable.Rows.Count > 0)
        {
            DataRow[] array = dataTable.Select(" PayStatus=0");
            if (array != null)
            {
                count += array.Length;
            }
            DataRow[] array2 = dataTable.Select(" Status=0");
            if (array2 != null)
            {
                count += array2.Length;
            }
            DataRow[] array3 = dataTable.Select("DeliverStatus=1");
            if (array3 != null)
            {
                count += array3.Length;
            }
        }
        this.Buyer_Order.Text = count.ToString();
    }

    #region 省市县联动
    protected void BindProList()
    {
        this.Province.DataSource = this.mycity.GetAllProvince();
        this.Province.DataBind();
    }
    protected void BindCityList()
    {
        this.City.DataSource = this.mycity.GetCity(this.Province.SelectedValue);
        this.City.DataBind();
    }
    protected void SelectCity(object sender, EventArgs e)
    {
        string selectedValue = this.Province.SelectedValue;
        this.BindCityList();
        this.BindAreaList();
    }
    protected void BindAreaList()
    {
        this.ddlArea.DataSource = this.mycity.GetAllAreaByCity(this.City.SelectedValue);
        this.ddlArea.DataBind();
    }
    protected void SelectArea(object sender, EventArgs e)
    {
        this.BindAreaList();
    } 
    #endregion

    protected void BindPages()
    {
        base.user.GetUserInfor();
        this.TxtU_TrueName.Text = base.user.UserInforRes["TrueName"].ToString();
        this.Add_Sex.SelectedValue = base.user.UserInforRes["Sex"].ToString();
        this.TxtU_Email.Text = base.user.UserEmail;
        this.OICQ.Text = base.user.UserInforRes["OICQ"].ToString();
        this.Province.SelectedValue = base.user.UserInforRes["Province"].ToString();
        this.BindCityList();
        this.City.SelectedValue = base.user.UserInforRes["City"].ToString();
        this.BindAreaList();
        this.ddlArea.SelectedValue = base.user.UserInforRes["CMArea"].ToString();
        this.TxtU_Address.Text = base.user.UserInforRes["Address"].ToString();
        this.ZipCode.Text = base.user.UserInforRes["ZipCode"].ToString();
        this.TxtU_MobilePhone.Text = base.user.UserInforRes["MobilePhone"].ToString();
        this.TxtU_HomePhone.Text = base.user.UserInforRes["HomePhone"].ToString();
        
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string text = this.Province.SelectedValue;
        string value;
        string value2;
        if (text == "")
        {
            text = base.user.UserInforRes["Province"].ToString();
            value = base.user.UserInforRes["City"].ToString();
            value2 = base.user.UserInforRes["CMArea"].ToString();
        }
        else
        {
            value = this.City.SelectedValue;
            value2 = this.ddlArea.SelectedValue;
        }
        IDbDataParameter[] myparam = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@Email", DbType.String, this.TxtU_Email.Text),
            this.conn.CreateParameter("@Sex", DbType.Int32, this.Add_Sex.SelectedValue)
		};
        string fields = "[Email]=@Email,[Sex]=@Sex";
        IDbDataParameter[] myparam_infr = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@TrueName", DbType.String, this.TxtU_TrueName.Text),
            this.conn.CreateParameter("@OICQ", DbType.String, this.OICQ.Text),
            this.conn.CreateParameter("@Province", DbType.String, text),
			this.conn.CreateParameter("@City", DbType.String, value),
			this.conn.CreateParameter("@CMArea", DbType.String, value2),
			this.conn.CreateParameter("@Address", DbType.String, this.TxtU_Address.Text),
			this.conn.CreateParameter("@ZipCode", DbType.String, this.ZipCode.Text),
            this.conn.CreateParameter("@MobilePhone", DbType.String, this.TxtU_MobilePhone.Text),
			this.conn.CreateParameter("@HomePhone", DbType.String, this.TxtU_HomePhone.Text)
		};
        string fields_infr = "[TrueName]=@TrueName,[OICQ]=@OICQ,[Province]=@Province,[City]=@City,[CMArea]=@CMArea,[Address]=@Address,[ZipCode]=@ZipCode,[MobilePhone]=@MobilePhone,[HomePhone]=@HomePhone ";
        UserBll userBll = new UserBll(this.conn);
        bool flag = false;
        try
        {
            flag = userBll.UpdateUser(base.user.UserID, fields, fields_infr, myparam, myparam_infr);
        }
        catch (Exception ex)
        {
            this.WriteErrMsg("", Language.GetMemberMsg("Common/EditFail", new string[]
            {
                ex.Message.ToString()
            }), "");
        }
        if (flag)
        {
            this.WriteSuccessMsg("", Language.GetMemberMsg("Common/EditSuccess", new string[0]), "Index.aspx");
        }
        else
        {
            this.WriteErrMsg("", Language.GetMemberMsg("Common/EditFail", new string[]
            {
                "提交数据不符合要求"
            }), "");
        }
    }
}